package cn.itrip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.comment.ItripListCommentVO;
import cn.itrip.beans.vo.comment.ItripScoreCommentVO;
import cn.itrip.beans.vo.comment.ItripSearchCommentVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.Page;
import cn.itrip.dao.service.ItripCommentService;
import cn.itrip.dao.service.ItripImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tequila
 **/
@RestController
@RequestMapping("/biz/api/comment")
@CrossOrigin
@Slf4j
public class ItripCommentController {

    @Autowired
    private ItripCommentService itripCommentService;
    @Autowired
    private ItripImageService itripImageService;

    @GetMapping("/getimg/{targetId}")
    public Dto getImg(@PathVariable Integer targetId){
        List<ItripImageVO> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("type",2);
            map.put("targetId",targetId);
            List<ItripImage> itripImageList = itripImageService.getImage(map);
            // 装入vo
            for (ItripImage itripImage : itripImageList) {
                ItripImageVO vo = new ItripImageVO();
                vo.setImgUrl(itripImage.getImgurl());
                vo.setPosition(itripImage.getPosition());
                list.add(vo);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常",ErrorCode.SYSTEM_ERROR);
        }
        return DtoUtil.returnDataSuccess(list);
    }

    @GetMapping("/getcount/{hotelId}")
    public Dto getCount(@PathVariable Integer hotelId){
        try {
            if (EmptyUtils.isNotEmpty(hotelId)){
                Map<String,Object> map = itripCommentService.getCount(hotelId);
                return DtoUtil.returnDataSuccess(map);

            }else{
                return DtoUtil.returnFail("酒店id不能为空", ErrorCode.PARAM_ISNULL);
            }
        } catch (Exception e){
            return DtoUtil.returnFail("系统错误",ErrorCode.SYSTEM_ERROR);
        }
    }

    @GetMapping("/gethotelscore/{hotelId}")
    public Dto getHotelScore(@PathVariable String hotelId){
        try {
            if (EmptyUtils.isNotEmpty(hotelId)){

                ItripScoreCommentVO itripScoreCommentVO = itripCommentService.getHotelScore(hotelId);
                return DtoUtil.returnDataSuccess(itripScoreCommentVO);
            }else{
                return DtoUtil.returnFail("酒店id不能为空", ErrorCode.PARAM_ISNULL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统错误",ErrorCode.SYSTEM_ERROR);
        }

    }

    @PostMapping("/getcommentlist")
    public Dto getCommentList(@RequestBody ItripSearchCommentVO itripSearchCommentVO){
        log.info("这是biz1模块加载的评论列表");
        Page<ItripListCommentVO> page =null;
        try {
            if(EmptyUtils.isNotEmpty(itripSearchCommentVO.getHotelId())){
                Map<String,Object> paramMap = new HashMap<>();
                paramMap.put("hotelId",itripSearchCommentVO.getHotelId());
                paramMap.put("pageNo",itripSearchCommentVO.getPageNo());
                paramMap.put("pageSize",itripSearchCommentVO.getPageSize());
                page = itripCommentService.getCommentList(paramMap);
                if (itripSearchCommentVO.getIsHavingImg() != -1){
                    paramMap.put("isHavingImg",itripSearchCommentVO.getIsHavingImg());
                    page = itripCommentService.getCommentList(paramMap);
                }else if(itripSearchCommentVO.getIsOk() != -1){
                    paramMap.put("isOk",itripSearchCommentVO.getIsOk());
                    page = itripCommentService.getCommentList(paramMap);
                }
                return DtoUtil.returnDataSuccess(page);
            }else{
                return DtoUtil.returnFail("酒店id不能为空", ErrorCode.PARAM_ISNULL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统错误",ErrorCode.SYSTEM_ERROR);
        }


    }
}
